package com.oristand.controller;

import com.oristand.pojo.Property;
import com.oristand.service.PropertyService;
import com.oristand.util.ResultResponse;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.CrossOrigin;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.bind.annotation.PathVariable;


/**
 * 产品的属性信息增删改查处理
 *
 * @author zhaijianfeng
 * @version 1.0
 * @date 2021/11/30
 */
@RestController
@CrossOrigin
public class PropertyController {

    /**
     * propertyService 操作产品属性信息的service接口
     */
    private PropertyService propertyService;

    @Autowired
    public void setPropertyService(PropertyService propertyService) {
        this.propertyService = propertyService;
    }

    /**
     * 测试地址: /findProperty/1
     * 根据属性id,查询产品的基本属性信息
     *
     * @param propertyId 产品属性信息id
     * @return ResultResponse 结果集 泛型是Property 如果查询成功,直接返回产品的基本属性信息,如果失败,则返回错误信息
     */
    @RequestMapping(path = "/findProperty/{propertyId}", method = RequestMethod.GET)
    public ResultResponse findProperty(@PathVariable("propertyId") Integer propertyId) {
        ResultResponse<Property> resultResponse = new ResultResponse<>();
        Property property = propertyService.findPropertyById(propertyId);
        if (property != null) {
            resultResponse.selectSuccess(property);
        } else {
            resultResponse.selectFail(ResultResponse.SELECT_FAIL);
        }
        return resultResponse;
    }

    /**
     * 测试地址: /deleteProperty/1
     * 根据属性id删除产品的属性信息
     *
     * @param propertyId 产品属性信息id
     * @return ResultResponse 结果集 指定泛型 Integer,如果i>0,删除成功, 返回删除所影响的行数;如果i=0删除失败,返回错误信息
     */
    @RequestMapping(path = "/deleteProperty/{propertyId}", method = RequestMethod.DELETE)
    public ResultResponse deleteProperty(@PathVariable("propertyId") Integer propertyId) {
        ResultResponse<Integer> resultResponse = new ResultResponse<>();
        int i = propertyService.deletePropertyById(propertyId);
        if (i > 0) {
            resultResponse.deleteSuccess(i);
        } else {
            resultResponse.deleteFail(ResultResponse.DELETE_FAIL);
        }
        return resultResponse;
    }

    /**
     * 测试地址: /saveProperty
     * 新增产品的属性信息
     *
     * @param property 产品属性信息
     * @return ResultResponse 结果集 指定泛型 Integer,如果新增属性信息成功,返回新增所影响的行数,失败则返回错误信息
     */
    @RequestMapping(path = "/saveProperty", method = RequestMethod.PUT)
    public ResultResponse saveProperty(Property property) {
        ResultResponse<Integer> resultResponse = new ResultResponse<>();
        int row = propertyService.saveProperty(property);
        if (row > 0) {
            resultResponse.insertSuccess(row);
        } else {
            resultResponse.insertFail(null);
        }
        return resultResponse;
    }

    /**
     * 测试地址: /updateProperty
     * 更新产品的属性信息
     *
     * @param property 产品属性信息
     * @return ResultResponse 结果集 指定泛型 Integer,如果更新成功,返回更新所影响的行数,失败则返回错误信息
     */
    @RequestMapping(path = "/updateProperty", method = RequestMethod.POST)
    public ResultResponse updateProperty(Property property) {
        int row = propertyService.updatePropertyById(property);
        ResultResponse<Integer> resultResponse = new ResultResponse<>();
        if (row > 0) {
            resultResponse.updateSuccess(row);
        } else {
            resultResponse.updateFail(ResultResponse.UPDATE_FAIL);
        }
        return resultResponse;
    }

}

